package cn.getech.data.development.utils.sched;

import cn.getech.data.development.entity.JobDepHistory;
import cn.getech.data.development.entity.JobNodeRunHistory;
import cn.getech.data.development.entity.JobRunHistory;
import cn.getech.data.development.entity.JobRunHistoryLogEntity;
import cn.getech.data.development.mapper.*;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/***
 * 保留最近两个月的日志
 */
@Slf4j
@Component
public class ClearJobLogUtils {

    @Autowired
    private JobDepRunHistoryMapper jobDepRunHistoryMapper;
    @Autowired
    private JobRunHistoryMapper jobRunHistoryMapper;
    @Autowired
    private JobNodeRunHistoryMapper jobNodeRunHistoryMapper;
    @Autowired
    private JobRunHistoryLogMapper jobRunHistoryLogMapper;
    @Autowired
    private JobDepHistoryLogMapper jobDepHistoryLogMapper;

    /***
     * 23:14分执行，保留最近180天的数据
     */
    public void clearWorkFlowLayLog() {
        //删除对应的作业流依赖的历史日志
        long begTime = System.currentTimeMillis() - 180 * 24 * 60 * 60 * 1000L;
        DateTime date = DateUtil.date(begTime);
        List<JobDepHistory> jobDepHistories = jobDepRunHistoryMapper.selectList(new QueryWrapper<JobDepHistory>().lt("begin_time", date));
        if (CollectionUtil.isNotEmpty(jobDepHistories)) {
            QueryWrapper<JobRunHistory> jobRunHistoryQueryWrapper = new QueryWrapper<>();
            QueryWrapper<JobRunHistoryLogEntity> jobRunHistoryLogEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<JobNodeRunHistory> jobNodeRunHistoryQueryWrapper = new QueryWrapper<>();
            for (JobDepHistory jobDepHistory : jobDepHistories) {
                try {
                    jobDepRunHistoryMapper.deleteById(jobDepHistory.getId());
                    jobDepHistoryLogMapper.deleteById(jobDepHistory.getId());
                    //删除对应的jobLog和jobNodeLog的记录数据
                    jobRunHistoryQueryWrapper.eq("dep_oozie_job_id", jobDepHistory.getOozieJobId());
                    List<JobRunHistory> jobRunHistories = jobRunHistoryMapper.selectList(jobRunHistoryQueryWrapper);
                    if(CollectionUtil.isNotEmpty(jobRunHistories)){
                        for (JobRunHistory jobRunHistory : jobRunHistories) {
                            jobRunHistoryMapper.deleteById(jobRunHistory);
                            jobRunHistoryLogEntityQueryWrapper.eq("id",jobRunHistory.getId());
                            jobRunHistoryLogMapper.delete(jobRunHistoryLogEntityQueryWrapper);
                            jobNodeRunHistoryQueryWrapper.eq("oozie_job_id", jobRunHistory.getOozieJobId());
                            List<JobNodeRunHistory> jobNodeRunHistories = jobNodeRunHistoryMapper.selectList(jobNodeRunHistoryQueryWrapper);
                            if(CollectionUtil.isNotEmpty(jobNodeRunHistories)){
                                List<Integer> ids = new ArrayList<>();
                                for (JobNodeRunHistory jobNodeRunHistory : jobNodeRunHistories) {
                                    ids.add(jobNodeRunHistory.getId());
                                }
                                jobNodeRunHistoryMapper.deleteBatchIds(ids);
                            }
                        }
                    }
                }catch ( Exception e){
                    log.error("删除作业流依赖的历史日志异常！error:{}",e.getMessage());
                }
            }
        }

    }


    /***
     * 23:23分执行，保留最近180天的数据
     */
    public void clearWorkFlowLog() {
        //删除对应的作业流依赖的历史日志
        long begTime = System.currentTimeMillis() - 180 * 24 * 60 * 60 * 1000L;
        DateTime date = DateUtil.date(begTime);
        List<JobRunHistory> jobRunHistories = jobRunHistoryMapper.selectList(new QueryWrapper<JobRunHistory>().lt("begin_time", date));
        if (CollectionUtil.isNotEmpty(jobRunHistories)) {
            QueryWrapper<JobRunHistoryLogEntity> jobRunHistoryLogEntityQueryWrapper = new QueryWrapper<>();
            QueryWrapper<JobNodeRunHistory> jobNodeRunHistoryQueryWrapper = new QueryWrapper<>();
            try {
                for (JobRunHistory jobRunHistory : jobRunHistories) {
                    jobRunHistoryMapper.deleteById(jobRunHistory);
                    jobRunHistoryLogEntityQueryWrapper.eq("id",jobRunHistory.getId());
                    jobRunHistoryLogMapper.delete(jobRunHistoryLogEntityQueryWrapper);
                    jobNodeRunHistoryQueryWrapper.eq("oozie_job_id", jobRunHistory.getOozieJobId());
                    List<JobNodeRunHistory> jobNodeRunHistories = jobNodeRunHistoryMapper.selectList(jobNodeRunHistoryQueryWrapper);
                    if(CollectionUtil.isNotEmpty(jobNodeRunHistories)){
                        List<Integer> ids = new ArrayList<>();
                        for (JobNodeRunHistory jobNodeRunHistory : jobNodeRunHistories) {
                            ids.add(jobNodeRunHistory.getId());
                        }
                        jobNodeRunHistoryMapper.deleteBatchIds(ids);
                    }
                }
            }catch ( Exception e){
                log.error("删除作业流的历史日志异常！error:{}",e.getMessage());
            }
        }

    }

}
